#include "iap.h"
#include "bsp/uart.h"
#include "bsp/board.h"
#include "serial.h"

#define _BOOT_VERSION	"V20241011"

const char iap_board_name[] __attribute__((at(CONFIG_FLASH_ADDR+0x800))) = CONFIG_BOARD_NAME;
const char iap_fw_version[] __attribute__((at(CONFIG_FLASH_ADDR+0xA00))) = "Loader_" _BOOT_VERSION;
const char iap_fw_name[] __attribute__((at(CONFIG_FLASH_ADDR+0xC00))) = "Boot";

void SysTick_Handler(void)
{
#ifdef HAL_MODULE_ENABLED
	HAL_IncTick();
#endif
	iap.ticks++;
	if (iap.run_app_delay > 0) {
		iap.run_app_delay--;
	}
}

int main(void)
{
	bsp_init_early();
	iap_boot(true);
	iap.state = IAP_ENTER;
	bsp_init();
#if CONFIG_SERIAL_TYPE > SERIAL_NONE
	uart_init();
#endif

#if CONFIG_CAN_ENABLE
	can_hw_init();
#endif

	while (1) {
		wdog_reload();

#if CONFIG_CAN_ENABLE
		can_poll();
#endif

#if CONFIG_SERIAL_TYPE > SERIAL_NONE
		serial_poll();
#endif
		if (iap.run_app_delay == 0) {
			iap_running_app();
			while(1);
		}
	}
}
